using UnityEngine;

public class PlayerLedgeClimbState : PlayerState
{
    private Vector2 _cornePos;
    private Vector2 _detectedPos;
    private bool _isClimbing;

    private bool _isHanging;
    private Vector2 _startPos;
    private Vector2 _stopPos;

    private int _Xinput;
    private int _Yinput;
    private Vector2 workspace;

    public PlayerLedgeClimbState(Player player, PlayerSM stateMachine, PlayerData playerData, string animBoolName,
        Core core) : base(player, stateMachine, playerData, animBoolName, core)
    {
    }

    public override void AnimationFinshTrigger()
    {
        base.AnimationFinshTrigger();

        _isAnimationFinshed = true;
        _player.Anim.SetBool("LedgeClimb", false);
    }

    public override void AnimationTrigger()
    {
        base.AnimationTrigger();

        _isHanging = true;
        _player.Anim.SetBool("Hanging", false);
    }

    public override void Enter()
    {
        base.Enter();
        _core.Movement.SetRigidbodyBodyTypeStatic();

        _player.transform.position = _detectedPos;
        _cornePos = DetermineCornePosition();

        _startPos.Set(_cornePos.x - _core.Movement.FacingDirection * _playerData._startOffset.x,
            _cornePos.y - _playerData._startOffset.y);
        _stopPos.Set(_cornePos.x + _core.Movement.FacingDirection * _playerData._stopOffset.x,
            _cornePos.y + _playerData._stopOffset.y);

        _player.transform.position = _startPos;
    }

    public override void Exit()
    {
        base.Exit();

        if (_isClimbing)
        {
            _isClimbing = false;
            _player.transform.position = _stopPos;
        }

        _core.Movement.SetRigidbodyBodyTypeDynamic();
        _isHanging = false;
    }

    public override void LogicUpdate()
    {
        base.LogicUpdate();


        if (!_isExitingState)
        {
            //if (!_isExitingState)
            //{
            //    _Xinput = LightInputManager.Instance.NormalInputX;
            //    _Yinput = LightInputManager.Instance.NormalInputY;

            //    if (_Xinput == _core.Movement.FacingDirection)
            //    {
            //        _isClimbing = true;
            //        _stateMachine.ChangeState(_player.IdleState);
            //    }
            //    else if (_Yinput < 0)
            //    {
            //        _stateMachine.ChangeState(_player.InAirState);
            //    }
            //}

            if (_isAnimationFinshed)
            {
                _stateMachine.ChangeState(_player.IdleState);
            }
            else
            {
                _Xinput = LightInputManager.Instance.NormalInputX;
                _Yinput = LightInputManager.Instance.NormalInputY;

                if (_Xinput == _core.Movement.FacingDirection && !_isClimbing) //DELETE: && _isHanging
                {
                    _isClimbing = true;
                    _player.Anim.SetBool("Hanging", false);
                    _player.Anim.SetBool("LedgeClimb", true);
                }
                else if (_Yinput < 0 && !_isClimbing) //DELETE: && _isHanging
                {
                    _stateMachine.ChangeState(_player.InAirState);
                }
            }
        }
    }

    public void SetDetectedPosition(Vector2 pos)
    {
        _detectedPos = pos;
    }

    public Vector2 DetermineCornePosition()
    {
        //���������ж����߼�����ӳ��ȵ�����������һ�����ݲ�
        var Xhit = Physics2D.Raycast(_core.Collision.WallCheck.position, Vector2.right * _core.Movement.FacingDirection,
            _core.Collision.WallCheckDistance, _core.Collision.WhatIsGround);
        var XDis = Xhit.distance;
        workspace.Set(XDis * _core.Movement.FacingDirection, 0f);

        var Yhit = Physics2D.Raycast(_core.Collision.LedgeCheckHorizontal.position + (Vector3)workspace, Vector2.down,
            _core.Collision.LedgeCheckHorizontal.position.y - _core.Collision.WallCheck.position.y,
            _core.Collision.WhatIsGround);
        var YDis = Yhit.distance;
        workspace.Set(_core.Collision.WallCheck.position.x + XDis * _core.Movement.FacingDirection,
            _core.Collision.LedgeCheckHorizontal.position.y - YDis);
        return workspace;
    }
}